<?php

namespace App\Http\Controllers\Common;

use App\Dwz;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Index\IndexController;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class CommonController extends Controller
{


    public function __construct()
    {
        header("content-type:text/html;charset=utf8");
    }

    public function unzip($src_file, $dest_dir = false, $create_zip_name_dir = true, $overwrite = true)
    {
        if ($zip = zip_open($src_file)) {
            if ($zip) {
                $splitter = ($create_zip_name_dir === true) ? "." : "/";
                if ($dest_dir === false) {
                    $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter)) . "/";
                }

                // 如果不存在 创建目标解压目录
                $this->create_dirs($dest_dir);

                // 对每个文件进行解压
                while ($zip_entry = zip_read($zip)) {
                    // 文件不在根目录
                    $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
                    if ($pos_last_slash !== false) {
                        // 创建目录 在末尾带 /
                        $this->create_dirs($dest_dir . substr(zip_entry_name($zip_entry), 0, $pos_last_slash + 1));
                    }

                    // 打开包
                    if (zip_entry_open($zip, $zip_entry, "r")) {

                        // 文件名保存在磁盘上
                        $file_name = $dest_dir . zip_entry_name($zip_entry);

                        // 检查文件是否需要重写
                        if ($overwrite === true || $overwrite === false && !is_file($file_name)) {
                            // 读取压缩文件的内容
                            $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));

                            @file_put_contents($file_name, $fstream);
                            // 设置权限
                            chmod($file_name, 0777);
                        }

                        // 关闭入口
                        zip_entry_close($zip_entry);
                    }
                }
                // 关闭压缩包
                zip_close($zip);
            }
        } else {
            return false;
        }
        return true;
    }

    /**
     * 创建目录
     */
    public function create_dirs($path)
    {
        if (!is_dir($path)) {
            $directory_path = "";
            $directories = explode("/", $path);
            array_pop($directories);

            foreach ($directories as $directory) {
                $directory_path .= $directory . "/";
                if (!is_dir($directory_path)) {
                    mkdir($directory_path);
                    chmod($directory_path, 0777);
                }
            }
        }
    }


    public function curl_get_sg($array, $timeout)
    {
        $realurl = array();
        $mh = curl_multi_init();//创建多个curl语柄
        foreach ($array as $k => $url) {
            $conn[$k] = curl_init($url);
            curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout);//设置超时时间
            curl_setopt($conn[$k], CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($conn[$k], CURLOPT_MAXREDIRS, 7);//HTTp定向级别
            curl_setopt($conn[$k], CURLOPT_NOBODY, 1);
            curl_setopt($conn[$k], CURLOPT_HEADER, 0);//这里不要header，加块效率
            curl_setopt($conn[$k], CURLOPT_FOLLOWLOCATION, 1); // 302 redirect
            curl_setopt($conn[$k], CURLOPT_RETURNTRANSFER, 1);
            curl_multi_add_handle($mh, $conn[$k]);
        }

        // 执行批处理句柄
        do {
            curl_multi_exec($mh, $active);
        } while ($active);

        foreach ($array as $k => $url) {
            $res[$k] = curl_multi_getcontent($conn[$k]);//获得返回信息
            $realurl[$k] = curl_getinfo($conn[$k], CURLINFO_EFFECTIVE_URL);//返回头信息
            curl_multi_remove_handle($mh, $conn[$k]);
            curl_close($conn[$k]);
        }


        curl_multi_close($mh);
        return $realurl;
    }

    public function curl_get($url)
    {

        $testurl = $url;
        $referer = 'http://www.360kan.com';
        $conputer_user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
        $mobile_user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $testurl);
        //参数为1表示传输数据，为0表示直接输出显示。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //curl_setopt($ch, CURLOPT_REFERER, $referer);
//        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, $conputer_user_agent);
        //参数为0表示不带头文件，为1表示带头文件
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    /*
     * url:访问路径
     * array:要传递的数组
     * */
    public function curl_post($url, $array)
    {
        $curl = curl_init();
        $user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";
        $mobile_user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4";
        $referer = "http://www.rartxt.com";
        $cookies = 'csrftoken=NK9zWiHi1QGpvvSYDk9zEmFNXfJ77bj77ZmTaEla5JgHDe1Cgw2UJNHvs6qIvaJa; sessionid=z8nncax7og8b3x74fw8nt2svp6l89pqa';
        //设置提交的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
        //curl_setopt($curl,CURLOPT_REFERER,$referer);
        //curl_setopt($curl, CURLOPT_COOKIE, $cookies);
        //设置post方式提交
        curl_setopt($curl, CURLOPT_POST, 1);
        //设置post数据
        $post_data = $array;
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
        //执行命令
        $data = curl_exec($curl);
        //关闭URL请求
        curl_close($curl);
        //获得数据并返回
        return $data;
    }

    public function curl_get_dwz($url)
    {

        $testurl = $url;
        $conputer_user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";
        $mobile_user_agent = "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $testurl);
        //参数为1表示传输数据，为0表示直接输出显示。
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_USERAGENT, $conputer_user_agent);
        //参数为0表示不带头文件，为1表示带头文件
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    #多线程请求
    public function curl_http($array, $timeout)
    {
        $res = array();
        $mh = curl_multi_init();//创建多个curl语柄
        foreach ($array as $k => $url) {
            $conn[$k] = curl_init($url);
            curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout);//设置超时时间
            curl_setopt($conn[$k], CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
            curl_setopt($conn[$k], CURLOPT_MAXREDIRS, 7);//HTTp定向级别
            curl_setopt($conn[$k], CURLOPT_NOBODY, 0);
            curl_setopt($conn[$k], CURLOPT_HEADER, 0);//这里不要header，加块效率
            curl_setopt($conn[$k], CURLOPT_RETURNTRANSFER, 1);
            curl_multi_add_handle($mh, $conn[$k]);
        }

        // 执行批处理句柄
        do {
            curl_multi_exec($mh, $active);
        } while ($active);

        foreach ($array as $k => $url) {
            $res[$k] = json_decode(curl_multi_getcontent($conn[$k]), 1);//获得返回信息
            curl_multi_remove_handle($mh, $conn[$k]);
            curl_close($conn[$k]);
        }


        curl_multi_close($mh);
        return $res;
    }

    #多线程post
    public function curl_post_http($array, $postdata, $timeout)
    {
        $res = array();
        $mh = curl_multi_init();//创建多个curl语柄
        foreach ($array as $k => $url) {
            $conn[$k] = curl_init();
            curl_setopt($conn[$k], CURLOPT_URL, $url);
            curl_setopt($conn[$k], CURLOPT_TIMEOUT, $timeout);//设置超时时间
            curl_setopt($conn[$k], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36');
            curl_setopt($conn[$k], CURLOPT_HEADER, 0);//这里不要header，加块效率
            curl_setopt($conn[$k], CURLOPT_RETURNTRANSFER, 1);
            //设置post方式提交
            curl_setopt($conn[$k], CURLOPT_POST, 1);
            //设置post数据
            curl_setopt($conn[$k], CURLOPT_POSTFIELDS, $postdata);
            //执行命令
            curl_multi_add_handle($mh, $conn[$k]);
        }

        // 执行批处理句柄
        do {
            curl_multi_exec($mh, $running);
            curl_multi_select($mh);
        } while ($running > 0);

        foreach ($array as $k => $url) {
            $res[$k] = curl_multi_getcontent($conn[$k]);//获得返回信息
            curl_multi_remove_handle($mh, $conn[$k]);
            curl_close($conn[$k]);
        }


        curl_multi_close($mh);
        return $res;
    }

    public function shorturl($longurl)
    {
        $jmlongurl = base64_encode($longurl);
        $dwz = new Dwz();
        $is = $dwz->where(['dwz_long_url' => $jmlongurl])->first();
        if ($is) {
            return base64_decode($is->dwz_url);
        }
        $url = 'http://api.t.sina.com.cn/short_url/shorten.json?source=' . config('wx.app_key') . '&url_long=' . urlencode($longurl);
        $urlarr = json_decode($this->curl_get_dwz($url), 1);
        $shorturl = $urlarr[0]['url_short'];
        $arr = ['dwz_long_url' => $jmlongurl, 'dwz_url' => base64_encode($shorturl)];
        foreach ($arr as $key => $v) {
            $dwz->$key = $v;
        }
        $res = $dwz->save();
        if ($res) {
            return $shorturl;
        } else {
            $this->shorturl($longurl);
        }


    }

}